/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.systemconfig.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.project.dao.DatasourceDao;
import io.renren.modules.project.dao.ModuleDao;
import io.renren.modules.project.entity.DatasourceEntity;
import io.renren.modules.project.entity.FrontendEntity;
import io.renren.modules.project.entity.ModuleEntity;
import io.renren.modules.systemconfig.dao.SysConfigServerDao;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.systemconfig.entity.ServerRuntime;
import io.renren.modules.systemconfig.entity.SystemConfigServersEntity;
import io.renren.modules.systemconfig.service.SysConfigServerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("sysConfigServerService")
public class SysConfigServerServiceImpl extends ServiceImpl<SysConfigServerDao, SystemConfigServersEntity> implements SysConfigServerService {
	@Autowired
	private DatasourceDao datasourceMapper;
	@Autowired
	private ModuleDao moduleMapper;
	@Autowired
	private RedisUtils redisUtils;
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = (String)params.get("key");

		IPage<SystemConfigServersEntity> page = this.page(
			new Query<SystemConfigServersEntity>().getPage(params),
			new QueryWrapper<SystemConfigServersEntity>()
				.like(StringUtils.isNotBlank(key),"name", key).or().like(StringUtils.isNotBlank(key),"ip_addr", key)
		);

		return new PageUtils(page);
	}
	@Override
	public PageUtils treeList(Map<String, Object> params) {
		String key = (String)params.get("key");
		IPage<SystemConfigServersEntity> page = this.page(
				new Query<SystemConfigServersEntity>().getPage(params),
				new QueryWrapper<SystemConfigServersEntity>()
						.like(StringUtils.isNotBlank(key),"name", key).or().like(StringUtils.isNotBlank(key),"ip_addr", key)
		);
		for(SystemConfigServersEntity sse:page.getRecords()){
			sse.setServerRuntime(JSONObject.parseObject(redisUtils.get("server-"+sse.getId()), ServerRuntime.class));
			sse.setDatasources(datasourceMapper.selectList(new QueryWrapper<DatasourceEntity>().eq("server_id",sse.getId())));
			sse.setModules(moduleMapper.selectList(new QueryWrapper<ModuleEntity>().eq("server_id",sse.getId())));
		}
		return new PageUtils(page);
	}

	@Override
	public SystemConfigServersEntity queryByKey(String key) {
		return baseMapper.queryByKey(key);
	}

	@Override
	@Transactional
	public void saveEntity(SystemConfigServersEntity entity) {
		this.baseMapper.insert(entity);
	}

	@Override
	@Transactional
	public void update(SystemConfigServersEntity user) {
		this.updateById(user);
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}




}